﻿using System;

namespace Serialization_Demo
{
    #region  定义一个序列化对象
    /// <summary>
    /// 定义一个序列化对象
    /// </summary>
    [Serializable]
    public class UserInfo
    {
        public string Name { get; set; }
        public Int32 Age { get; set; }
        public bool IsVip { get; set; }
        /// <summary>
        /// 必须提供无参数的构造方法，以便于XML序列化
        /// </summary>
        public UserInfo() { }
        public UserInfo(string name, int age, bool isvip)
        {
            this.Name = name;
            this.Age = age;
            this.IsVip = isvip;
        }
    }
    #endregion

    /// <summary>
    /// 序列化与反序列化
    /// </summary>
    class Serialization_Demo
    {
        /// <summary>
        /// 序列化方法
        /// </summary>
        /// <param name="user"></param>
        public static void BinarySerialization(UserInfo user)
        {
            using (System.IO.FileStream fs = new System.IO.FileStream("MySerialization.bin", System.IO.FileMode.Create))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                //执行二进制序列化
                formatter.Serialize(fs, user);
            }
        }

        /// <summary>
        /// 反序列化方法
        /// </summary>
        /// <param name="user"></param>
        public static UserInfo BinaryDesearyization()
        {
            using (System.IO.FileStream fs = new System.IO.FileStream("MySerialization.bin", System.IO.FileMode.OpenOrCreate, 
                System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                //执行二进制反序列化
                UserInfo user = formatter.Deserialize(fs) as UserInfo;
                return user;
            }
        }

        /// <summary>
        /// 简单测试
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            UserInfo user = new UserInfo("小雨", 26, true);
            //执行序列化
            BinarySerialization(user);
            //执行反序列化
            ///执行目录里面会多一个"MySerialization.bin"文件
            UserInfo user2 = BinaryDesearyization();

            Console.WriteLine("Name：{0}\nAge：{1}\nIsVip：{2}", user2.Name, user2.Age, user2.IsVip);
            Console.WriteLine(Environment.NewLine);


          
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(UserInfo));
            //执行XML序列化
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("MySerializer.xml"))
            {
                //执行XML序列化
                serializer.Serialize(sw, user);
            }

            using (System.IO.FileStream fs = new System.IO.FileStream("MySerializer.xml",System.IO.FileMode.OpenOrCreate))
            {
                UserInfo user3 = serializer.Deserialize(fs) as UserInfo;
                Console.WriteLine("Name：{0}\nAge：{1}\nIsVip：{2}", user3.Name, user3.Age, user3.IsVip);
                Console.WriteLine(Environment.NewLine);
            }

            Console.ReadKey();
        }
    }


}
